package controllers
import(
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"

)

type LoginController struct {
	beego.Controller
}

func (this *LoginController) Get(){
	Isexit :=this.Input().Get("exit") =="true"

	if Isexit {

		username := "lll"
		pwd := "lll"
		maxAge := 1
		this.Ctx.SetCookie("uname",username,maxAge,"/")
		this.Ctx.SetCookie("pwd",pwd,maxAge,"/")
		this.Redirect("/topic",301)
		return
	}


	this.TplName = "login.html"
}
func (this *LoginController) Post(){
	//username :=this.Input().Get("uname")
	//pwd :=this.Input().Get("pwd")
	//this.Ctx.WriteString(fmt.Sprint(this.Input()))//map[pwd:[hellwelrlwer] autologin:[on] uname:[liwotian]]

	//return

	username :=this.Input().Get("uname")
	pwd :=this.Input().Get("pwd")
	autlogin := this.Input().Get("autologin") =="on"


	if beego.AppConfig.String("uname") == username  &&
		beego.AppConfig.String("pwd") == pwd{
			maxAge :=0
			if autlogin {
				maxAge = 1<<31 - 1  //左移动
			}
			this.Ctx.SetCookie("uname",username,maxAge,"/")
			this.Ctx.SetCookie("pwd",pwd,maxAge,"/")


	}

	this.Redirect("/",301)
	return   // 不加 又会渲染一次 浪费资源


}


func  checkAccount(ctx  *context.Context) bool{
		ck,err := ctx.Request.Cookie("uname")
		if err != nil{
			return false
		}


		uname := ck.Value

		ck,err = ctx.Request.Cookie("pwd")

		if err != nil{
			return false
		}
		pwd := ck.Value


		return beego.AppConfig.String("uname") == uname  &&
		beego.AppConfig.String("pwd") == pwd
}

